package com.blackcet.wholeexecption.vo;

import com.blackcet.wholeexecption.execption.BaseCodeInfo;
import com.blackcet.wholeexecption.enums.ErrorCode;
import com.blackcet.wholeexecption.enums.ResultCode;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@NoArgsConstructor
public class Result<T> implements Serializable {

    /** 返回状态码 */
    private String code;

    /** 提示信息 */
    private String msg;

    /** 数据类型 */
    private T data;

    public Result(String code,String message) {
        this.code = code;
        this.msg = message;
    }

    public Result(BaseCodeInfo resultCode) {
        this.code = resultCode.code();
        this.msg = resultCode.message();
    }

    public Result(BaseCodeInfo resultCode,T data) {
        this(resultCode);
        this.data = data;
    }

    /** 快速创建成功结果 */
    public static final Result SUCCESS = new Result(ResultCode.SUCCESS);
    /** 快速创建成功结果并返回结果数据 */
    public static Result SUCCESS(Object data) {
        return new Result(ResultCode.SUCCESS,data);
    }
    /** 快速创建失败结果(返回 500：系统异常) */
    public static final Result ERROR = new Result(ErrorCode.INTERNAL_SERVER_ERROR);
    /** 快速创建失败结果并返回结果数据(返回 500：系统异常) */
    public static Result error(Object data) {
        return new Result(ErrorCode.INTERNAL_SERVER_ERROR,data);
    }
    /** 自定义异常 */
    public static Result error(String code,String message) {
        return new Result(code,message);
    }

    /** 返回结果包含数据 */
    public static Result result(ResultCode resultCode,Object data){
        return new Result(resultCode,data);
    }
    /** 返回结果不包含数据 */
    public static Result result(ResultCode resultCode){
        return new Result(resultCode);
    }
}
